// cloudfunctions/getGoodsCollectCount/index.js
const cloud = require('wx-server-sdk');
cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
  })
const db = cloud.database();

exports.main = async (event, context) => {
  try {
    const { goodsId } = event;
    if (!goodsId) {
      return { success: false, message: '缺少商品ID参数' };
    }
    
    // 直接从blindbox_goods集合获取收藏数
    const goodsRes = await db.collection('blindbox_goods').doc(goodsId).get();
    if (!goodsRes.data) {
      return { success: false, message: '商品不存在' };
    }
    
    return { 
      success: true, 
      count: goodsRes.data.collectCount || 0,
      message: '获取收藏数成功'
    };
  } catch (error) {
    console.error('获取商品收藏数失败', error);
    return { 
      success: false, 
      message: error.message || '服务器错误',
      error: error.message
    };
  }
};